package problem309;

//309.买卖股票的最佳时机
//https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-cooldown/
class Solution {
    public int maxProfit(int[] prices) {
        int n = prices.length;
        int[][] dp = new int[n + 1][2];
        dp[1][0] = -prices[0];
        dp[1][1] = 0;
        for (int i = 2; i <= n; i++) {
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 2][1] - prices[i-1]);
            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i-1]);
        }
        return dp[n][1];
    }
}

/*
 * 
 * dp[i][0]:第i天时, 状态为买入股票 的最大收入
 * dp[i][1]:第i天时, 状态为卖出股票 的最大收入
 * 
 * dp[i][0] = Math.max(dp[i-1][0], dp[i-2][1]-prices[i]);
 * dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0]+prices[i]);
 * 
 * 
 */